package com.imooc.demo.web;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.imooc.demo.entity.Board;
import com.imooc.demo.entity.Send;
import com.imooc.demo.entity.UserInfo;
import com.imooc.demo.service.BoardService;
import com.imooc.demo.service.SendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/board")
public class BoardController {
    @Autowired
    BoardService boardService;
    @Autowired
    SendService sendService;

    ///////创建留言
    @PostMapping("/messagesinsert")
    public Map<String, Object> messagesInsert(@RequestParam("buyerOpenid") String buyerOpenid,
                                           @RequestParam("userMessages") String userMessages) {

        System.out.println("进来了");
        Board insert_board = new Board();
        insert_board.setBuyerOpenid(buyerOpenid);
        insert_board.setUserMessages(userMessages);

        System.out.println("insert_board:"+insert_board.toString());
        // 添加到数据库
        Boolean flag = boardService.messagesInsert(insert_board);
        if(!flag){
            System.out.println("插入数据库board失败！");
        }else {
            System.out.println("插入数据库board成功！");
        }
        // 封装返回小程序
        Map<String, Object> result = new HashMap<>();
        result.put("success", flag);
        return  result;
    }

    //通过buyerOpenid获取留言信息
    @RequestMapping(value = "findmessagesbyopenid",method = RequestMethod.GET)
    private  Map<String,Object>  findMessagesByOpenid( @RequestParam("buyerOpenid") String buyerOpenid
                                                ){
        Map<String,Object>  modelMap = new HashMap<String ,Object>();
        List<Board> boardList= boardService.findMessagesByOpenid(buyerOpenid);

        System.out.println("boardList:"+boardList.toString());
        modelMap.put("boardList",boardList);
        return modelMap;
    }


    //用户删除留言
    @RequestMapping(value = "/messagesdelete",method = RequestMethod.POST)
    //@RequestBody标识接收的内容除了xwww from url encoding 编码内容之外的一些内容，如json，xml等,此处需传入xml
    private  Map<String,Object>  messagesDelete(@RequestParam("boardId") Integer boardId){
        Map<String,Object>  modelMap = new HashMap<String ,Object>();
        Board board = boardService.findMessagesById(boardId);
        board.setBoardDelete(1);

        modelMap.put("success",boardService.messagesDelete(board));
        return modelMap;
    }


    /////答复
    //修改种类信息
    @RequestMapping(value = "/replyinsert",method = RequestMethod.POST)
    //@RequestBody标识接收的内容除了xwww from url encoding 编码内容之外的一些内容，如json，xml等
    private  Map<String,Object>  replyInsert(
                                              @RequestParam("boardId") Integer boardId,
                                              @RequestParam("reply") String reply
                                             )
            throws JsonParseException, JsonMappingException, IOException {
        Map<String,Object>  modelMap = new HashMap<String ,Object>();
        Board board=boardService.findMessagesById(boardId);

        board.setReply(reply);

        modelMap.put("success",boardService.replyInsert(board));
        return modelMap;
    }



    //通过buyerOpenid获取答复信息
    @RequestMapping(value = "getreply",method = RequestMethod.GET)
    private  Map<String,Object>  getReply( @RequestParam("buyerOpenid") String buyerOpenid){
        Map<String,Object>  modelMap = new HashMap<String ,Object>();
        List<UserInfo> boardList= boardService.getReply(buyerOpenid);
        System.out.println("boardList:"+boardList.toString());
        modelMap.put("boardList",boardList);
        return modelMap;
    }




    @RequestMapping(value = "getunread",method = RequestMethod.GET)
    private  Map<String,Object>  getUnRead( @RequestParam("buyerOpenid") String buyerOpenid){
        Map<String,Object>  modelMap = new HashMap<String ,Object>();
        List<Board> boardList= boardService.getUnRead(buyerOpenid);

        List<Send> sends=sendService.findUnread(buyerOpenid);
        /*int number1=0;
        int number2=0;
        if(boardList!=null){
           // number1=boardList.get(0).getBoards().size();
        }
        if(sends!=null){
            number2=sends.size();
        }
*/
        Integer number = boardList.size()+sends.size();
        System.out.println("boardList:"+boardList );
        modelMap.put("number", number);
        return modelMap;
    }


    //设置消息阅读状态
    @RequestMapping(value = "/setreadstatus",method = RequestMethod.POST)
    //@RequestBody标识接收的内容除了xwww from url encoding 编码内容之外的一些内容，如json，xml等
    private  Map<String,Object> setReadStatus(@RequestParam("boardId") Integer boardId) {
        Map<String,Object>  modelMap = new HashMap<String ,Object>();
        Board board = boardService.findMessagesById(boardId);

        board.setBoardLabel(1);
        System.out.println(board);
        //修改种类信息 ，"success"保存操作状态，如category1Service.modifyCategory1(category1)操作成功返回true，否则false
        modelMap.put("success",boardService.setReadStatus(board));
        return modelMap;
    }



    //通过buyerOpenid获取留言信息
    @RequestMapping(value = "getboardbyboardid",method = RequestMethod.GET)
    private  Map<String,Object>  getBoardByBoardId( @RequestParam("boardId") Integer boardId
    ){
        Map<String,Object>  modelMap = new HashMap<String ,Object>();
        List<Board> boardList= boardService.getBoardByBoardId(boardId);

        System.out.println("boardList:"+boardList.toString());
        modelMap.put("boardList",boardList);
        return modelMap;
    }





}
